﻿using DayuCode.Designer.Common;
using DayuCode.Designer.Entity;
using DayuCode.Designer.Service;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DayuCode.Designer.Forms.template
{
    public partial class FormPageComponent : UIForm
    {
        long pageLayoutId;
        long? pageComponentId;
        TemplatePageComponent pageComponent;
        public FormPageComponent(long pageLayoutId, long? pageComponentId)
        {
            InitializeComponent();
            this.pageLayoutId = pageLayoutId;
            this.pageComponentId = pageComponentId;
            pageComponent = TemplatePageComponentHandler.getTemplatePageComponent(pageComponentId);
            if (pageComponentId == null)
            {
                pageComponent.LayoutId = pageLayoutId;
            }
            cmbComponentType.DataSource = pageComponent.ComponentTypes;

            txtComponentName.Text = pageComponent.ComponentName;
            txtComponentCode.Text = pageComponent.ComponentCode;
            txtOrdnum.Text = "" + pageComponent.Ordnum;
            int idx = pageComponent.ComponentTypes.IndexOf(new CodeItem(pageComponent.ComponentType, ""));
            if (idx >= 0)
            {
                cmbComponentType.SelectedIndex = idx;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if(txtComponentName.Text.Trim().Length == 0)
            {
                this.ShowErrorTip("名称不能为空!");
                return;
            }
            pageComponent.ComponentName = txtComponentName.Text.Trim();
            pageComponent.ComponentCode = txtComponentCode.Text.Trim();
            pageComponent.Ordnum = Cast.ToInt32(txtOrdnum.Text, 0);
            pageComponent.ComponentType = (cmbComponentType.SelectedItem as CodeItem).Code;
            TemplatePageComponentHandler.saveTemplatePageComponent(pageComponent);

            Close();
            DialogResult = DialogResult.OK;
        }

        public TemplatePageComponent TemplatePageComponent
        {
            get { return pageComponent; }
        }
    }
}
